<?php

namespace App\Services\Store\User;

use Exception;

class RanchService extends BaseUserService
{

    /**
     * 牧场列表
     * @return array
     * @throws Exception
     */
    public function list(): array
    {
        // 查询牧场列表
        $storeRanchList = $this->getStoreRanchModel()->list([
            'id', 'name', 'province_id', 'city_id', 'region_id', 'address'
        ]);

        $provinceIds = array_column($storeRanchList, 'province_id');
        $cityIds = array_column($storeRanchList, 'city_id');
        $regionIds = array_column($storeRanchList, 'region_id');
        $areaIds = array_merge($provinceIds, $cityIds, $regionIds);

        $areaList = $this->getAreaModel()->getAreaNameListByCodeList($areaIds);
        $areaListMap = array_column($areaList, 'name', 'code');

        $list = [];
        foreach ($storeRanchList as $item) {

            $areaName = $this->getAreaName($areaListMap[$item['province_id']], $areaListMap[$item['city_id']], $areaListMap[$item['region_id']]);

            $list[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'address' => $areaName . ' ' . $item['address'],
            ];
        }

        return [
            'list' => $list,
        ];
    }
}
